import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import qiankunPlugin from "vite-plugin-qiankun";
// import prefixer from "postcss-prefix-selector"; //样式隔离

// https://vite.dev/config/
export default ({ mode }) => {
  const isDev = mode === "development";
  const host = "127.0.0.1",
    port = 8012,
    appName = "vite-demo";
  return defineConfig({
    base: isDev ? `http://${host}:${port}/${appName}` : `/micro-app/${appName}`,
    server: {
      port,
      cors: true, //允许跨域
      origin: `http//${host}:${port}`, // 必须配置，否则无法访问静态资源
    },
    plugins: [
      vue(),
      qiankunPlugin(appName, { useDevMode: isDev }),
      /** 
      prefixer({
        prefix: "[data-qiankun-app]", // 这里的值要和 main.tsx 中的属性名保持一致
        transform(prefix, selector, prefixedSelector, filePath, rule) {
          if (selector.match(/^(html|body)/)) {
            return selector.replace(/^([^\s]*)/, `$1 ${prefix}`);
          }

          if (filePath.match(/node_modules/)) {
            return selector; // Do not prefix styles imported from node_modules
          }

          const annotation = rule.prev();
          if (
            annotation?.type === "comment" &&
            annotation.text.trim() === "no-prefix"
          ) {
            return selector; // Do not prefix style rules that are preceded by: 
          }

          return prefixedSelector;
        },
      }),
      */
    ],
  });
};
